package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.GoodsBean;
import com.project.mapper.IGoodsMapper;
import com.project.service.IGoodsService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;
import java.util.List;

public class GoodsServiceImpl extends BaseService implements IGoodsService {

    @Override
    public void add(GoodsBean goodsBean) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.add(goodsBean);
        session.commit();
        session.close();
    }

    @Override
    public void del(int id) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(int id, int price) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.update(id,price);
        session.commit();
        session.close();
    }

    @Override
    public GoodsBean findById(int id) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        GoodsBean goods = mapper.findById(id);
        session.close();
        return goods;
    }

    @Override
    public CutPageBean<GoodsBean> cutByPrice(int pageNo, String name, int minPrice, int maxPrice) {
        CutPageBean<GoodsBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        List<GoodsBean> list = mapper.cutByPrice(name,minPrice,maxPrice,
                (pageNo-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE);
        cutBean.setList(list);
        cutBean.setCount(mapper.countByPrice(name,minPrice,maxPrice));
        session.close();
        return cutBean;
    }

    @Override
    public CutPageBean<GoodsBean> cutByDate(int pageNo, String factory, LocalDate startDate, LocalDate endDate) {
        CutPageBean<GoodsBean> cutBean = new CutPageBean<>();
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        List<GoodsBean>list = mapper.cutByDate(factory,startDate,endDate,
                (pageNo-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE);
        cutBean.setList(list);
        cutBean.setCount(mapper.countByDate(factory,startDate,endDate));
        session.close();
        return cutBean;
    }
}
